# -*- coding:utf-8 -*-
from numpy import source
from service.aliyun.cdn import checkDomain,getCheckValue,addCdnDomain,BatchSetCdnDomainServerCertificate,DescribeCdnDomainDetail
from service.aliyun.domain import aliParsing
import json
import time

#校验域名所有权
def checkDomainName(value):
    cdn_key = value["cdn_access_key"]["key"]
    cdn_secret = value["cdn_access_key"]["secret"]
    domain_key = value["domain_access_key"]["key"]
    domain_secret = value["domain_access_key"]["secret"]
    domain_name = value["domain_name"]
    try:
        checkDomain(key=cdn_key,secret=cdn_secret,domain=domain_name)
    except:
        print("【错误】域名归属权，校验失败，尝试校验！")
        parsing_value = json.loads(getCheckValue(key=cdn_key,secret=cdn_secret,domain=domain_name))['Content']
        aliParsing(key=domain_key,secret=domain_secret,domain_rr="verification",domain_type="TXT",domain=domain_name,value=parsing_value)
        time.sleep(60)  #等待域名的解析生效
        try:
            checkDomain(key=cdn_key,secret=cdn_secret,domain=domain_name)
        except Exception as es:
            print('【错误】域名归属权，校验失败:%s'%es)

#创建cdn加速
def createCdnDomain(value):
    for i in value:
        cdn_key = i["key"]
        cdn_secret = i["secret"]
        cdn_type = i["cdn_type"]
        domain_name = i["domain_name"]
        sources = i["sources"]
        try:
            addCdnDomain(key=cdn_key,secret=cdn_secret,cdn_type=cdn_type,domain_name=domain_name,sources=sources)
            print("【信息】添加成功。")
        except Exception as es:
            if "exist" in str(es):
                print("【错误】该配置早已存在：%s"%es)
            else:
                print("【错误】添加cdn加速失败：%s"%es)

#批量添加配置域名https
def batchConfigHttps(value):
    for i in value:
        domain_name = i["domain"]
        https_pem = i["ssl_pem"]
        https_key = i["ssl_key"]
        cdn_key = i["key"]
        cdn_secret = i["secret"]
        try:
            BatchSetCdnDomainServerCertificate(key=cdn_key,secret=cdn_secret,domain_name=domain_name,sslpri=https_key,sslpub=https_pem)
            print("【信息】https开启成功。")
        except Exception as es:
            print("【错误】证书配置异常：%s"%es)

#cdn加速域名进行云解析
def cdnParsingDomain(value):
    for i in value:
        domain_name = i["domain"]
        domain_name_root = str(domain_name).split('.',1)[1]
        domain_name_head = str(domain_name).split('.',1)[0]
        cdn_key = i["cdn_access_key"]["key"]
        cdn_secret = i["cdn_access_key"]["secret"]
        domain_key = i["domain_access_key"]["key"]
        domain_secret = i["domain_access_key"]["secret"]
        cname = json.loads(DescribeCdnDomainDetail(key=cdn_key,secret=cdn_secret,domain_name=domain_name))["GetDomainDetailModel"]["Cname"]
        try:
            aliParsing(key=domain_key,secret=domain_secret,domain=domain_name_root,domain_rr=domain_name_head,domain_type='CNAME',value=cname)
        except Exception as es:
            print("【错误】解析cdn的cname值失败：%s"%es)

def run(values):
    print("【信息】进入cdn处理模块：")
    for key,value in values.items():
        if key == "verify_ownership":
            checkDomainName(value)
        elif key == "add_cdn_domain":
            createCdnDomain(value)
        elif key == "cdn_cert_domain":
            batchConfigHttps(value)
        elif key == 'cdn_doamin_parsing':
            cdnParsingDomain(value)
        else:
            print('【错误】无效yaml规则。。。。')